<script setup>
import { ref, watchEffect } from "vue";
import * as echarts from "echarts";

const props = defineProps({
  label: {
    type: String,
    default: "",
  },
  value: {
    type: Number,
    default: 0,
  },
  sum: {
    type: Number,
    default: 100,
  },
  unit: {
    type: String,
    default: "个",
  },
});

const chartRef = ref(null);

watchEffect(() => {
  if (!chartRef.value) return;
  const chart = echarts.init(chartRef.value);
  const option = {
    grid: {
      width: "78%",
      bottom: 55,
      left: 40,
      right: "0%",
    },
    title: [
      {
        text: `41`,
        x: "81%",
        y: "10%",
        textStyle: {
          fontFamily: "Helvetica-Bold",
          fontWeight: "bold",
          fontSize: 12,
          color: "rgba(201,233,248,0.6)",
        },
      },
    ],
    xAxis: {
      show: false,
      type: "value",
      // boundaryGap: [0, 0],
      max: props.sum,
    },
    yAxis: {
      type: "category",
      axisTick: "none",
      axisLine: "none",
      show: true,
      position: "right",
      axisLabel: {
        padding: [0, -50, -10, 0],
        align: "right",
        verticalAlign: "bottom",
        textStyle: {
          color: "#FFFFFF",
          fontSize: 18,
          fontWeight: "bold",
          rich: {
            unit: {
              color: "rgba(201,233,248,0.8)",
              fontSize: 12,
            },
          },
        },
        formatter: function (value) {
          return `${value} {unit|${props.unit}}`;
        },
      },
      data: [props.value],
    },
    series: [
      {
        // 进度
        data: [{ name: props.label, value: props.value }],
        name: props.label,
        type: "bar",
        barWidth: 10,
        showBackground: true,
        backgroundStyle: {
          borderRadius: 0,
          color: "rgba(255,255,255,0.08)",
          borderColor: "#252F3D",
          borderWidth: 0,
          opacity: 1,
        },
        itemStyle: {
          color: {
            x: 1,
            y: 0,
            x2: 0,
            y2: 0,
            colorStops: [
              {
                offset: 0,
                color: "#2DFFF8",
              },
              {
                offset: 1,
                color: "rgba(45,255,248,0.1)",
              },
            ],
          },
          borderRadius: 0,
        },
        label: {
          show: true,
          position: [0, -40],
          fontSize: 10,
          color: "rgba(201,233,248,0.8)",
          padding: [42, 0, 0, -40],
          formatter: "{b}",
        },
        emphasis: {
          disabled: true,
        },
        markPoint: {
          symbol:
            "image://",
          symbolSize: [15, 35],
          label: { show: false },
          symbolOffset: [0, 0],
          data: [{ name: "最大值", type: "max" }],
        },
      },
    ],
  };
  chart.setOption(option);
});
</script>

<template>
  <div ref="chartRef" style="width: 100%; height: 100%"></div>
</template>
